<?php
/**
 * Worder Content Platform
 *
 * @author		Magki <magkir@163.com>
 * @copyright	Copyright &copy; 2011 Magki Studio
 * @license		GNU General Public License 2.0
 * @version		$Id: CDatabase.php 7 2011-08-16 08:11:57Z magkir@163.com $
 * @package		base
 */

/**
 * 数据获取方法类
 *
 * @package Database
 */

class CDatabase
{
    /**
     * 数据库连接对象
     */
	private $_connect;
	
    /**
     * 数据库配置数据
	 * 
     * @var Config
     */
	private static $_config;
	
	/**
     * 数据库对象的实例
	 * 
     * @var CDatabase
     */
    private static $_instance;
	
	/**
	 * 数据库类构造函数
	 */
	private function __construct()
	{
		if ($this->_connect = @mysql_connect($this->_config['host'], $this->_config['user'], $this->_config['password'], true)) {
            if (@mysql_select_db($this->_config['name'], $this->_connect)) {
				mysql_query("SET NAMES 'utf8'", $this->_connect);
				echo "db is ok!";
            }
		}
	}
	
	/**
	 * 数据库克隆函数，防止对象被克隆
	 * 
	 * @return
	 */
	private function __clone()
	{
		return;
	}

	/**
     * 获取数据库实例化对象
     *
     * @return CDatabase
     */
    public static function get($config)
    {
		//填充数据库配置数据
		self::$_config = require $config;
		
        if (empty(self::$_instance)) {
			self::$_instance = new self();
        }

        return self::$_instance;
    }
}
?>
